In [37]:
%matplotlib inline
from __future__ import division


fs = 8000.0
f0 = 256.0  # Hz
duration = 0.05 # 1.0

t = np.linspace(0, duration, num=fs*duration)
N_overtones = 7
harmonics = np.arange(1, N_overtones)

f = f0 * harmonics;
print f

# exponentially decaying gain
G = 0.5 * np.exp(-f/1e3) + 0.5
# G = np.ones_like(f)

x_full = np.sum(np.exp(2j*np.pi*np.outer(f,t)), 0)/N_overtones
x_missing1 = np.sum(G[1:]*np.exp(2j*np.pi*np.outer(t, f[1:])), 1)/N_overtones
x_missing2 = np.sum(G[2:]*np.exp(2j*np.pi*np.outer(t, f[2:])), 1)/N_overtones
x_missing3 = np.sum(G[3:]*np.exp(2j*np.pi*np.outer(t, f[3:])), 1)/N_overtones


from scipy.io import wavfile


plt.figure(figsize=(16,4))

ax1 = plt.subplot(1,4,1)
plt.plot(t, np.real(x_full))

ax2 = plt.subplot(1,4,2, sharex=ax1, sharey=ax1)
plt.plot(t, np.real(x_missing1))

ax3 = plt.subplot(1,4,3, sharex=ax1, sharey=ax1)
plt.plot(t, np.real(x_missing2))

ax4 = plt.subplot(1,4,4, sharex=ax1, sharey=ax1)
plt.plot(t, np.real(x_missing3))

plt.xlim(0, 0.02)
plt.ylim(-1, 1)


[  256.   512.   768.  1024.  1280.  1536.]
Out[37]:
(-1, 1)

In [23]:
from IPython.display import Audio

Audio(np.real(x_full), rate=fs)


Out[23]:

In [24]:
Audio(np.real(x_missing1), rate=fs)


Out[24]:

In [25]:
Audio(np.real(x_missing2), rate=fs)


Out[25]:

In [26]:
Audio(np.real(x_missing3), rate=fs)


Out[26]:

In [5]:
from pygrfnn import Zparam, GrFNN
from pygrfnn.network import Model, make_connections
from pygrfnn.vis import tf_detail, tf_simple

# params = Zparam(alpha=0.02, beta1=0.0, beta2=.0, epsilon=1.0)
# params = Zparam(alpha=0.01, beta1=-15.0, beta2=-5, epsilon=1.0)
# params = Zparam(alpha=-0.5, beta1=10.0, beta2=-3.0, epsilon=1.0)

params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
# params2 = Zparam(alpha=-0.01, beta1=5, beta2=-3.0, epsilon=1.0)
params2 = Zparam(alpha=-0.2, beta1=0, beta2=0.0, epsilon=1.0)


# freqs = (128, 2048)
# num_oscs = 256

freqs = (128, 2048)
num_oscs = 256

stimulus_conn_type = 'linear'  # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2

# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'

In [10]:
# create model
g = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
mf = Model()
mf.add_layer(g, input_channel=0)



M = make_connections(g, g, modes=[1/3, 1/2, 1, 2, 3])
# mf.connect_layers(g, g, M, connection_type=intra_layer_conn_type, self_connect=False)

# print mf.connections[g][0].monomials[64].indices.shape

# run it
mf.run(gain*x_full, t, 1/fs)

# show TFR
tf_detail(g.Z, t, g.f, t_detail=0.08, title="All Harmonics")


2000/2000 done!
Out[10]:
(<matplotlib.figure.Figure at 0x108169050>,
 <matplotlib.image.AxesImage at 0x11d7c5c90>,
 [<matplotlib.lines.Line2D at 0x108fd8750>],
 None,
 [<matplotlib.lines.Line2D at 0x108dc50d0>])

This one is kind of working (do not touch!)

(it worked for threeFreqMonomials(..., N=10, tol=0.5e-3, ...)


In [28]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.2, beta1=0, beta2=0.0, epsilon=1.0)


# freqs = (128, 2048)
# num_oscs = 256

freqs = (128, 2048)
num_oscs = 256

stimulus_conn_type = 'linear'  # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.4

# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'

# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)

M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
M2 = make_connections(g2, g2)

m1.connect_layers(g1, g2, M1, weight=500, connection_type=intra_layer_conn_type, self_connect=True)
m1.connect_layers(g2, g2, M2, weight=-10, connection_type='all2freq', self_connect=True)

# run it
m1.run(gain*x_missing1, t, 1/fs)

# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")


a) Elapsed: 4.16587781906 secs
b) Elapsed: 4.27566385269 secs
c) Elapsed: 21.1212158203 secs
d) Elapsed: 22.5995368958 secs
e) Elapsed: 34.4100677967 secs
f) Elapsed: 38.8860969543 secs
g) Elapsed: 39.6458568573 secs
400/400 done!
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-6cb33b8b2f27> in <module>()
     35 # show TFR
     36 tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
---> 37 tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")

/Users/jorgeh/Documents/CCRMA/research/pygrfnn/pygrfnn/vis.pyc in mpl_wrapper(*args, **kwargs)
     52     def mpl_wrapper(*args, **kwargs):
     53         if MPL:
---> 54             output = fun(*args, **kwargs)
     55         else:
     56             logging.info('Skipping call to %s() (couldn\'t import Matplotib'

/Users/jorgeh/Documents/CCRMA/research/pygrfnn/pygrfnn/vis.pyc in tf_detail(TF, t, f, title, t_detail, x, display_op, cmap, figsize)
    219         t_detail, idx = find_nearest(t, t_detail)
    220         # axF.invert_xaxis()
--> 221         detail = axF.semilogy(opTF[:, idx], f)
    222         axF.set_yticks(nice_freqs)
    223         axF.get_yaxis().set_major_formatter(mpl.ticker.ScalarFormatter())

/usr/local/lib/python2.7/site-packages/matplotlib/axes.pyc in semilogy(self, *args, **kwargs)
   4375         b = self._hold
   4376         self._hold = True  # we've already processed the hold
-> 4377         l = self.plot(*args, **kwargs)
   4378         self._hold = b  # restore the hold
   4379 

/usr/local/lib/python2.7/site-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
   4139             lines.append(line)
   4140 
-> 4141         self.autoscale_view(scalex=scalex, scaley=scaley)
   4142         return lines
   4143 

/usr/local/lib/python2.7/site-packages/matplotlib/axes.pyc in autoscale_view(self, tight, scalex, scaley)
   1961                 y1 += delta
   1962             if not _tight:
-> 1963                 y0, y1 = ylocator.view_limits(y0, y1)
   1964             self.set_ybound(y0, y1)
   1965 

/usr/local/lib/python2.7/site-packages/matplotlib/ticker.pyc in view_limits(self, vmin, vmax)
   1483         if minpos <= 0 or not np.isfinite(minpos):
   1484             raise ValueError(
-> 1485                 "Data has no positive values, and therefore can not be "
   1486                 "log-scaled.")
   1487 

ValueError: Data has no positive values, and therefore can not be log-scaled.

In [51]:
plt.figure(figsize=(14,6))
ax1 = plt.subplot(1,2,1)
ax1.semilogx(g2.f, np.abs(g2.Z[:,-1]), 'b', label="layer2")
ax1.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
for tl in ax1.get_yticklabels():
    tl.set_color('b')

ax2 = ax1.twinx()
ax2.semilogx(g1.f, np.abs(g1.Z[:,-1]), 'g' ,label="layer1")
for tl in ax2.get_yticklabels():
    tl.set_color('g')
    
ax1.axis('tight')
    
plt.subplot(1,2,2)
plt.semilogx(g2.f, np.abs(g2.Z[:,-1]))
plt.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
plt.xlim(200, 300)


Out[51]:
(200, 300)

In [20]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.5, beta1=0.5, beta2=-0.1, epsilon=1.0)


# freqs = (128, 2048)
# num_oscs = 256

freqs = (128, 2048)
num_oscs = 257

stimulus_conn_type = 'linear'  # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2

# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'

# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)

M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
# M2 = make_connections(g2, g2)

m1.connect_layers(g1, g2, M1, weight=100, connection_type=intra_layer_conn_type, self_connect=True)
# m1.connect_layers(g2, g1, M2, weight=1, connection_type='all2freq', self_connect=True)

# run it
m1.run(gain*x_missing1, t, 1/fs)

# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")


400/400 done!
Out[20]:
(<matplotlib.figure.Figure at 0x121e9af90>,
 <matplotlib.image.AxesImage at 0x122b5d050>,
 [<matplotlib.lines.Line2D at 0x10c20ee90>],
 None,
 [<matplotlib.lines.Line2D at 0x10c322810>])

In [14]:
# plt.plot(t, np.abs(g2.Z[64,:]))
tf_detail(g2.Z, t, g2.f, t_detail=[0.03, 0.04, 0.05], title="Missing fundamental")


Out[14]:
(<matplotlib.figure.Figure at 0x122300650>,
 <matplotlib.image.AxesImage at 0x10c78bed0>,
 [<matplotlib.lines.Line2D at 0x1346bd310>,
  <matplotlib.lines.Line2D at 0x1229ad450>,
  <matplotlib.lines.Line2D at 0x1229ad5d0>],
 None,
 [<matplotlib.lines.Line2D at 0x121ce4810>,
  <matplotlib.lines.Line2D at 0x134531e10>,
  <matplotlib.lines.Line2D at 0x134532090>])

In [15]:
plt.figure(figsize=(14,6))
ax1 = plt.subplot(1,2,1)
ax1.semilogx(g2.f, np.abs(g2.Z[:,-1]), 'b', label="layer2")
ax1.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
for tl in ax1.get_yticklabels():
    tl.set_color('b')

ax2 = ax1.twinx()
ax2.semilogx(g1.f, np.abs(g1.Z[:,-1]), 'g' ,label="layer1")
for tl in ax2.get_yticklabels():
    tl.set_color('g')
    
ax1.axis('tight')
    
plt.subplot(1,2,2)
plt.semilogx(g2.f, np.abs(g2.Z[:,-1]))
plt.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
plt.xlim(200, 300)


Out[15]:
(200, 300)

In [29]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.5, beta1=0.5, beta2=-0.1, epsilon=1.0)


# freqs = (128, 2048)
# num_oscs = 256

freqs = (128, 2048)
num_oscs = 257

stimulus_conn_type = 'linear'  # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2

# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'

# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)

M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
# M2 = make_connections(g2, g2)

m1.connect_layers(g1, g2, M1, weight=100, connection_type=intra_layer_conn_type, self_connect=True)
# m1.connect_layers(g2, g1, M2, weight=1, connection_type='all2freq', self_connect=True)

# run it
m1.run(gain*x_missing2, t, 1/fs)

# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")


400/400 done!
Out[29]:
(<matplotlib.figure.Figure at 0x1344c7510>,
 <matplotlib.image.AxesImage at 0x10cd68790>,
 [<matplotlib.lines.Line2D at 0x10c1b4750>],
 None,
 [<matplotlib.lines.Line2D at 0x10c1bd9d0>])

In [38]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.5, beta1=0.5, beta2=-0.1, epsilon=1.0)


# freqs = (128, 2048)
# num_oscs = 256

freqs = (128, 2048)
num_oscs = 257

stimulus_conn_type = 'linear'  # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2

# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'

# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)

M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
# M2 = make_connections(g2, g2)

m1.connect_layers(g1, g2, M1, weight=100, connection_type=intra_layer_conn_type, self_connect=True)
# m1.connect_layers(g2, g1, M2, weight=1, connection_type='all2freq', self_connect=True)

# run it
m1.run(gain*x_missing3, t, 1/fs)

# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")


400/400 done!
Out[38]:
(<matplotlib.figure.Figure at 0x11eda9210>,
 <matplotlib.image.AxesImage at 0x134b9d690>,
 [<matplotlib.lines.Line2D at 0x134bc3510>],
 None,
 [<matplotlib.lines.Line2D at 0x11e8b2790>])

In [ ]: